package org.acooly.secretbox.service.impl;

import java.io.File;
import java.io.InputStream;
import java.util.Collections;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.acooly.android.utils.StringUtils;
import org.acooly.secretbox.service.FileSystem;
import org.acooly.secretbox.service.domain.FileObject;
import org.acooly.secretbox.service.support.FileObjectTypeComparator;

import android.content.Context;
import android.os.Environment;
import android.util.Log;

public class LocalFileSystem implements FileSystem {

	private static final String TAG = "LocalFileSystem";
	private Context context;

	private String root;

	public LocalFileSystem(Context context) {
		this.context = context;
		root = getExternalStorage();
		Log.i(TAG, "ROOT:" + root);
	}

	@Override
	public FileObject createFile(String path, Map<String, String> metadata, InputStream in) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public FileObject getFile(String path) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void mkdir(String path, Map<String, String> metadata) {
		// TODO Auto-generated method stub

	}

	@Override
	public void rm(String absolutePath) {
		// TODO Auto-generated method stub

	}

	@Override
	public List<FileObject> list(String path) {

		if (StringUtils.isBlank(path) || path.indexOf(root) == -1) {
			path = root;
		}

		List<FileObject> objects = new LinkedList<FileObject>();
		// 如果是子目录则添加返回对象
		if (!path.equals(root)) {
			FileObject back = new FileObject(FileObject.TYPE_BACK);
			File file = new File(path);
			back.setPath(file.getParent());
			file = null;
			objects.add(back);
		}
		File current = new File(path);
		if (!current.exists()) {
			throw new RuntimeException("目录不存在:" + path);
		}
		if (!current.isDirectory()) {
			throw new RuntimeException("目标路径不是目录:" + path);
		}
		try {
			File[] files = current.listFiles();
			for (File file : files) {
				objects.add(FileToObject(file));
			}
		} catch (Exception e) {
			throw new RuntimeException("浏览目录失败:" + e.getMessage());
		}
		Collections.sort(objects, new FileObjectTypeComparator());
		return objects;
	}

	private String getExternalStorage() {
		String state = Environment.getExternalStorageState();
		if (!Environment.MEDIA_MOUNTED.equals(state)) {
			throw new RuntimeException("扩展存储没有准备好。");
		}
		return Environment.getExternalStorageDirectory().getPath();
	}

	private FileObject FileToObject(File file) {
		FileObject object = null;
		if (file.isFile()) {
			object = new FileObject(FileObject.TYPE_FILE);
		} else {
			object = new FileObject(FileObject.TYPE_FOLDER);
		}
		object.setName(file.getName());
		object.setPath(file.getPath());
		object.setContentType(getMIMEType(file));
		Date lastModifyTime = new Date(file.lastModified());
		object.setLastModifyTime(lastModifyTime);
		object.setCreateTime(lastModifyTime);
		object.setLength(file.length());
		object.setStatus(0);
		return object;
	}

	private String getMIMEType(File f) {
		String type = "";
		String fName = f.getName();
		String end = fName.substring(fName.lastIndexOf(".") + 1, fName.length()).toLowerCase();
		if (end.equals("m4a") || end.equals("mp3") || end.equals("mid") || end.equals("xmf") || end.equals("ogg") || end.equals("wav")) {
			type = "audio";
		} else if (end.equals("3gp") || end.equals("mp4")) {
			type = "video";
		} else if (end.equals("jpg") || end.equals("gif") || end.equals("png") || end.equals("jpeg") || end.equals("bmp")) {
			type = "image";
		} else {
			type = "*";
		}
		type += "/*";
		return type;
	}

}
